package _test;

import gui.Guimap;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import javax.swing.JFrame;
import javax.swing.JPanel;

import pathfinding.Pathfinding;

import main.Coord;

public class TestGui {
	private static final Color WIN = Color.GREEN;
	private static int WID=Guimap.MAP_WIDTH;
	private static int HEI=Guimap.MAP_HEIGHT;
	private int TILE_WIDTH=10;
	private int TILE_HEIGHT=10;
	private Color DEFAULT=Color.WHITE;
	private Color LOOK=Color.BLUE;
	private Color CRASH=Color.RED;
	private Color AIM=Color.ORANGE;
	private Color OBST=Color.BLACK;
	private Color STAND=Color.MAGENTA;
	private Color[][] field;
	private MapPane pane;
	private List<Coord> path;
	
	public TestGui() {
		pane = new MapPane();
		pane.setPreferredSize(new Dimension(WID*TILE_WIDTH, HEI*TILE_HEIGHT));
		JFrame frame = new JFrame("Test");
		frame.add(pane);
		frame.pack();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//		frame.setVisible(true);
		
		field = new Color[WID][HEI];
		for (int i=0; i<field.length; i++) {
			for (int j=0; j<field[0].length; j++) {
				field[i][j] = DEFAULT;
			}
		}
	}
	
	public void setObstacles(boolean[][] obst) {
		for (int i=0; i<obst.length; i++) {
			for (int y=0; y<obst[i].length; y++) {
				if (obst[i][y]) {
					field[i][y] = OBST;
				}
			}
		}
		pane.repaint();
	}
	private void wash() {
		for (int x=0; x<WID; x++) {
			for (int y=0; y<HEI; y++) {
				if (!field[x][y].equals(OBST)) {
					field[x][y] = DEFAULT;
				}
			}
		}
	}
	
	private void sleep(long t) {
//		try { Thread.sleep(t/100);
//		} catch (InterruptedException e) { }
	}
	
	public void look(Coord c) {
		sleep(100);
		if (!field[c.x][c.y].equals(STAND)) {
			field[c.x][c.y] = LOOK;
		}
		pane.repaint();
	}
	public void lookFail(Coord c) {
		wash();
		field[c.x][c.y] = CRASH;
		pane.repaint();
		sleep(1000);
		
		field[c.x][c.y] = OBST;
		pane.repaint();
	}
	public void tooClose(Coord c) {
		field[c.x][c.y]= CRASH;
		pane.repaint();
		sleep(1000);
		wash();
		pane.repaint();
	}
	public void stand(Coord c) {
		wash();
		field[c.x][c.y] = STAND;
		pane.repaint();
		sleep(1000);
	}
	public void win(Coord c) {
		field[c.x][c.y] = WIN;
		pane.repaint();
		sleep(1000);
		wash();
	}
	public void aim(Coord c) {
		field[c.x][c.y]= AIM;
		pane.repaint();
	}
	public void done(List<Coord> list) {
		path = new ArrayList<Coord>();
		for (Coord c: list) {
			path.add(new Coord(c.x*TILE_WIDTH+TILE_WIDTH/2, c.y*TILE_HEIGHT+TILE_HEIGHT/2));
		}
		pane.repaint();
	}

	
	public static void main(String[] args) {
		Scanner sc=null;
		try { sc = new Scanner(new File("map.txt"));
		} catch (FileNotFoundException e) { e.printStackTrace(); }
		
		
		boolean[][] paths = new boolean[WID][HEI];
		int y=0;
		while(sc.hasNextLine()) {
			String line = sc.nextLine();
			for (int x=0; x<line.length(); x++) {
				paths[x][y] = line.charAt(x)=='x';
			}
			y++;
		}
		
		Pathfinding.find(new Coord(0, 5), new Coord(49, 9), paths);
	}
	
	@SuppressWarnings("serial")
	private class MapPane extends JPanel {
		
		@Override
		public void paintComponent(Graphics g) {
			System.out.println("h�r");
			g.setColor(Color.BLACK);
			g.fillRect(0, 0, WID*TILE_WIDTH, HEI*TILE_HEIGHT);
			
			for (int y=0; y<HEI; y++) {
				for (int x=0; x<WID; x++) {
					g.setColor(field[x][y]);
					g.fillRect(x*TILE_WIDTH+1, y*TILE_HEIGHT+1, TILE_WIDTH-2, TILE_HEIGHT-2);
				}
			}
			if (path!=null) {
				g.setColor(Color.BLUE);
				for (int i=1; i<path.size(); i++) {
					g.drawLine(path.get(i-1).x, path.get(i-1).y, path.get(i).x, path.get(i).y);
				}
			}
		}
		
	}

}
